/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22652
 * Date: 2024-03-31
 * Time: 21:45
 */
public class test_5 {
    static boolean isQuit = false;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            while (!isQuit) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("66666666666666");
                    e.printStackTrace();
                }
            }
        });
        t.start();

        // 主线程这里执行一些其他逻辑之后, 要让 t 线程结束.
        Thread.sleep(3000);

        // 这个代码就是在修改前面设定的标志位.
        isQuit = true;
        System.out.println("把 t 线程终止");
    }
}
/*public static boolean finalIsQuit = false;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while (!finalIsQuit){
                System.out.println("通过共享的标记来进行沟通");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("66666666666666666666");
                }
            }
        });
        t.start();
        System.out.println("未中断");
        Thread.sleep(3000);
        finalIsQuit = true;
        System.out.println("已中断");*/